/**********************************************************************************
  Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.

  (c) Copyright 1996 - 2002  Gary Henderson (gary.henderson@ntlworld.com),
                             Jerremy Koot (jkoot@snes9x.com)

  (c) Copyright 2002 - 2004  Matthew Kendora

  (c) Copyright 2002 - 2005  Peter Bortas (peter@bortas.org)

  (c) Copyright 2004 - 2005  Joel Yliluoma (http://iki.fi/bisqwit/)

  (c) Copyright 2001 - 2006  John Weidman (jweidman@slip.net)

  (c) Copyright 2002 - 2006  funkyass (funkyass@spam.shaw.ca),
                             Kris Bleakley (codeviolation@hotmail.com)

  (c) Copyright 2002 - 2007  Brad Jorsch (anomie@users.sourceforge.net),
                             Nach (n-a-c-h@users.sourceforge.net),
                             zones (kasumitokoduck@yahoo.com)

  (c) Copyright 2006 - 2007  nitsuja


  BS-X C emulator code
  (c) Copyright 2005 - 2006  Dreamer Nom,
                             zones

  C4 x86 assembler and some C emulation code
  (c) Copyright 2000 - 2003  _Demo_ (_demo_@zsnes.com),
                             Nach,
                             zsKnight (zsknight@zsnes.com)

  C4 C++ code
  (c) Copyright 2003 - 2006  Brad Jorsch,
                             Nach

  DSP-1 emulator code
  (c) Copyright 1998 - 2006  _Demo_,
                             Andreas Naive (andreasnaive@gmail.com)
                             Gary Henderson,
                             Ivar (ivar@snes9x.com),
                             John Weidman,
                             Kris Bleakley,
                             Matthew Kendora,
                             Nach,
                             neviksti (neviksti@hotmail.com)

  DSP-2 emulator code
  (c) Copyright 2003         John Weidman,
                             Kris Bleakley,
                             Lord Nightmare (lord_nightmare@users.sourceforge.net),
                             Matthew Kendora,
                             neviksti


  DSP-3 emulator code
  (c) Copyright 2003 - 2006  John Weidman,
                             Kris Bleakley,
                             Lancer,
                             z80 gaiden

  DSP-4 emulator code
  (c) Copyright 2004 - 2006  Dreamer Nom,
                             John Weidman,
                             Kris Bleakley,
                             Nach,
                             z80 gaiden

  OBC1 emulator code
  (c) Copyright 2001 - 2004  zsKnight,
                             pagefault (pagefault@zsnes.com),
                             Kris Bleakley,
                             Ported from x86 assembler to C by sanmaiwashi

  SPC7110 and RTC C++ emulator code
  (c) Copyright 2002         Matthew Kendora with research by
                             zsKnight,
                             John Weidman,
                             Dark Force

  S-DD1 C emulator code
  (c) Copyright 2003         Brad Jorsch with research by
                             Andreas Naive,
                             John Weidman

  S-RTC C emulator code
  (c) Copyright 2001-2006    byuu,
                             John Weidman

  ST010 C++ emulator code
  (c) Copyright 2003         Feather,
                             John Weidman,
                             Kris Bleakley,
                             Matthew Kendora

  Super FX x86 assembler emulator code
  (c) Copyright 1998 - 2003  _Demo_,
                             pagefault,
                             zsKnight,

  Super FX C emulator code
  (c) Copyright 1997 - 1999  Ivar,
                             Gary Henderson,
                             John Weidman

  Sound DSP emulator code is derived from SNEeSe and OpenSPC:
  (c) Copyright 1998 - 2003  Brad Martin
  (c) Copyright 1998 - 2006  Charles Bilyue'

  SH assembler code partly based on x86 assembler code
  (c) Copyright 2002 - 2004  Marcus Comstedt (marcus@mc.pp.se)

  2xSaI filter
  (c) Copyright 1999 - 2001  Derek Liauw Kie Fa

  HQ2x, HQ3x, HQ4x filters
  (c) Copyright 2003         Maxim Stepin (maxim@hiend3d.com)

  Win32 GUI code
  (c) Copyright 2003 - 2006  blip,
                             funkyass,
                             Matthew Kendora,
                             Nach,
                             nitsuja

  Mac OS GUI code
  (c) Copyright 1998 - 2001  John Stiles
  (c) Copyright 2001 - 2007  zones


  Specific ports contains the works of other authors. See headers in
  individual files.


  Snes9x homepage: http://www.snes9x.com

  Permission to use, copy, modify and/or distribute Snes9x in both binary
  and source form, for non-commercial purposes, is hereby granted without
  fee, providing that this license information and copyright notice appear
  with all copies and any derived work.

  This software is provided 'as-is', without any express or implied
  warranty. In no event shall the authors be held liable for any damages
  arising from the use of this software or it's derivatives.

  Snes9x is freeware for PERSONAL USE only. Commercial users should
  seek permission of the copyright holders first. Commercial use includes,
  but is not limited to, charging money for Snes9x or software derived from
  Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
  using Snes9x as a promotion for your commercial product.

  The copyright holders request that bug fixes and improvements to the code
  should be forwarded to them so everyone can benefit from the modifications
  in future versions.

  Super NES and Super Nintendo Entertainment System are trademarks of
  Nintendo Co., Limited and its subsidiary companies.
**********************************************************************************/

/**********************************************************************************
  Snes9x GTK Port
  
  (c) Copyright 2007 - 2008  Brandon Wright (bearoso@gmail.com)
**********************************************************************************/

#include <gdk/gdkx.h>
#include <gdk/gdkkeysyms.h>
#include <glade/glade-build.h>

#include "gtk_s9x.h"
#include "gtk_preferences.h"
#include "gtk_icon.h"
#include "gtk_display.h"
#include "gtk_file.h"
#include "gtk_sound.h"
#include "gtk_control.h"
#include "gtk_cheat.h"

static gboolean 
event_main_window_delete (GtkWidget *widget, 
                          GdkEvent *event, 
                          gpointer data)
{
    S9xExit ();
    gtk_main_quit ();
    
    return TRUE;
}

static gboolean
event_continue_item_activate (GtkWidget *widget, gpointer data)
{
    Snes9xWindow *window = (Snes9xWindow *) data;
    
    window->unpause_from_user ();
    
    return TRUE;
}

static gboolean
event_open_cheats (GtkWidget *widget, gpointer data)
{
    Snes9xCheats *cheats;
    cheats = new Snes9xCheats ();
    cheats->show ();
    delete cheats;

    return TRUE;
}

static gboolean
event_pause_item_activate (GtkWidget *widget, gpointer data)
{
    Snes9xWindow *window = (Snes9xWindow *) data;
        
    window->pause_from_user ();
    
    return TRUE;
}

static gboolean
event_drawingarea_expose (GtkWidget *widget,
                          GdkEvent *event,
                          gpointer data)
{
    Snes9xWindow *window = (Snes9xWindow *) data;
    
    window->config->window_width =
        GTK_WIDGET (window->window)->allocation.width;
    window->config->window_height =
        GTK_WIDGET (window->window)->allocation.height;
    
    S9xDisplayRefresh (window->last_width, window->last_height);
    
    if (window->is_paused ()) 
    {
        S9xDeinitUpdate (window->last_width, window->last_height);
    }
        
    return FALSE;
}

static gboolean
event_key (GtkWidget *widget, GdkEventKey *event, gpointer data)
{
    Binding b;
    
    b = BINDING_KEY;
    
    /* Provide escape key to get out of fullscreen */
    if (event->keyval == GDK_Escape &&
        top_level->is_fullscreen)
    {
        top_level->leave_fullscreen_mode ();
    }
    
    b |= (BINDING_KEY_MASK & event->keyval);
    
    if (event->state & GDK_SHIFT_MASK)
        b |= BINDING_SHIFT;
        
    if (event->state & GDK_CONTROL_MASK)
        b |= BINDING_CTRL;
        
    if (event->state & GDK_MOD1_MASK)
        b |= BINDING_ALT;
        
    S9xReportButton (b, (event->type == GDK_KEY_PRESS));
    
    return FALSE; /* Pass the key to GTK */
}

static void
event_double_size (GtkWidget *widget, gpointer data)
{
    GtkWidget *item;
    int y_padding = 0;

    Snes9xWindow *window = (Snes9xWindow *) data;
    
    item = glade_xml_get_widget (window->glade, "menubar");
    y_padding += item->allocation.height;
    
    item = glade_xml_get_widget (window->glade, "statusbar");
    y_padding += item->allocation.height;
    
    gtk_window_resize (GTK_WINDOW (window->window), 512, 224 * 2 + y_padding);
    
    return;
}

gboolean 
event_motion_notify (GtkWidget *widget, 
                     GdkEventMotion *event,
                     gpointer user_data)
{
    Snes9xWindow *window = (Snes9xWindow *) user_data;
    
    int c_width, c_height; 
    c_width = GTK_WIDGET (window->drawing_area)->allocation.width;
    c_height = GTK_WIDGET (window->drawing_area)->allocation.height;
    
    window->mouse_loc_x = (uint16) 
        ((int) (event->x) - window->mouse_region_x) * 
        256 / window->mouse_region_width; 
    
    window->mouse_loc_y = (uint16) 
        ((int) (event->y) - window->mouse_region_y) * SNES_HEIGHT_EXTENDED / 
        (window->mouse_region_height == 0 ? 1 : window->mouse_region_height);   
       
    /*printf ("Reporting %d, %d for mouse position\n", 
            (uint16) ((int) event->x - window->mouse_region_x) *
                256 / window->mouse_region_width,
            (uint16) ((int) event->y - window->mouse_region_y) * 
                SNES_HEIGHT_EXTENDED / window->mouse_region_height); */  
                          
    
    return FALSE;
}

gboolean 
event_button_press (GtkWidget *widget,
                    GdkEventButton *event,
                    gpointer user_data)
{
    switch (event->button)
    {
        case 1:
            S9xReportButton (BINDING_MOUSE_BUTTON0, 1);
            break;
        case 2:
            S9xReportButton (BINDING_MOUSE_BUTTON1, 1);
            break;
        case 3:
            S9xReportButton (BINDING_MOUSE_BUTTON2, 1);
            break;
    }
    
    return FALSE;
}

gboolean 
event_button_release (GtkWidget *widget,
                      GdkEventButton *event,
                      gpointer user_data)
{
    switch (event->button)
    {
        case 1:
            S9xReportButton (BINDING_MOUSE_BUTTON0, 0);
            break;
        case 2:
            S9xReportButton (BINDING_MOUSE_BUTTON1, 0);
            break;
        case 3:
            S9xReportButton (BINDING_MOUSE_BUTTON2, 0);
            break;
    }
     
    return FALSE;
}

static void
event_fullscreen (GtkWidget *widget, gpointer data)
{
    Snes9xWindow *window = (Snes9xWindow *) data;
    
    if (!window->is_fullscreen) 
        window->enter_fullscreen_mode ();
    else
        window->leave_fullscreen_mode ();
    
    return;
}

static void
event_native_size (GtkWidget *widget, gpointer data)
{
    GtkWidget *item;
    int y_padding = 0;

    Snes9xWindow *window = (Snes9xWindow *) data;
    
    item = glade_xml_get_widget (window->glade, "menubar");
    y_padding += item->allocation.height;
    
    item = glade_xml_get_widget (window->glade, "statusbar");
    y_padding += item->allocation.height;
    
    gtk_window_resize (GTK_WINDOW (window->window), 256, 224 + y_padding);
    
    return;
}

static void
event_record_movie (GtkWidget *widget, gpointer data)
{
    if (S9xMovieActive ()) 
        S9xMovieStop (FALSE);

    S9xMovieCreate (S9xChooseMovieFilename (FALSE),
                    0xFF,
                    MOVIE_OPT_FROM_RESET,
                    NULL, 
                    0);
    return;
}

static void
event_open_movie (GtkWidget *widget, gpointer data)
{
    if (S9xMovieActive ()) 
        S9xMovieStop (FALSE);
    
    S9xMovieOpen (S9xChooseMovieFilename (TRUE), FALSE);
    
    return;
}

static void
event_stop_recording (GtkWidget *widget, gpointer data)
{
    if (S9xMovieActive ()) 
        S9xMovieStop (FALSE);
    
    return;
}

static void
event_jump_to_frame (GtkWidget *widget, gpointer data)
{
    Snes9xWindow *window = (Snes9xWindow *) data;
    
    window->movie_seek_dialog ();
    
    return;
}
    
static void
event_reset (GtkWidget *widget, gpointer data)
{
    S9xSoftReset ();
    
    return;
}

static void
event_hard_reset (GtkWidget *widget, gpointer data)
{
    S9xReset ();
    
    return;
}

static void
event_save_state (GtkWidget *widget, gpointer data)
{
    int slot;
    char *name = (char *) gtk_widget_get_name (widget);
    
    slot = atoi (&(name[11]));
     
    S9xQuickSaveSlot (slot);
    
    return;
}

static void
event_save_state_file (GtkWidget *widget, gpointer data)
{
    ((Snes9xWindow *) data)->save_state_dialog ();
    
    return;
}

static void
event_load_state (GtkWidget *widget, gpointer data)
{
    int slot;
    char *name = (char *) gtk_widget_get_name (widget);
     
    slot = atoi (&(name[11]));
     
    S9xQuickLoadSlot (slot);
    
    return;
}

static void
event_load_state_file (GtkWidget *widget, gpointer data)
{
    ((Snes9xWindow *) data)->load_state_dialog ();
 
    return;
}

static void
event_open_rom (GtkWidget *widget, gpointer data)
{
    ((Snes9xWindow *) data)->open_rom_dialog ();
    
    return;
}

static void
event_recent_open (GtkRecentChooser *chooser, gpointer data)
{
    Snes9xWindow *window = (Snes9xWindow *) data;
    gchar *uri = gtk_recent_chooser_get_current_uri (chooser);
    gchar *filename = g_filename_from_uri (uri, NULL, NULL);
    
    window->try_open_rom (filename);
    
    g_free (filename);
    g_free (uri);
    
    return;
}

static void
event_save_spc (GtkWidget *widget, gpointer data)
{
    ((Snes9xWindow *) data)->save_spc_dialog ();
    
    return;
}

static void
event_port (GtkWidget *widget, gpointer data)
{   
    const gchar *name = gtk_widget_get_name (widget);
    
    if (!(gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget))))
        return;
    
    if (!strcasecmp (name, "joypad1"))
    {
        S9xSetController (0, CTL_JOYPAD, 0, 0, 0, 0);
    }
    
    else if (!strcasecmp (name, "joypad2"))
    {
        S9xSetController (1, CTL_JOYPAD, 1, 0, 0, 0);
    }
    
    else if (!strcasecmp (name, "mouse1"))
    {
        S9xSetController (0, CTL_MOUSE, 0, 0, 0, 0);
    }
    
    else if (!strcasecmp (name, "mouse2"))
    {
        S9xSetController (1, CTL_MOUSE, 0, 0, 0, 0);
    }
    
    else if (!strcasecmp (name, "superscope1"))
    {
        S9xSetController (0, CTL_SUPERSCOPE, 0, 0, 0, 0);
    }
    
    else if (!strcasecmp (name, "superscope2"))
    {
        S9xSetController (1, CTL_SUPERSCOPE, 0, 0, 0, 0);
    }
    
    else if (!strcasecmp (name, "multitap1"))
    {
        S9xSetController (0, CTL_MP5, 0, 1, 2, 3);
    }
    
    else if (!strcasecmp (name, "multitap2"))
    {
        S9xSetController (1, CTL_MP5, 4, 5, 6, 7);
    }
    
    return;
}

Snes9xWindow::Snes9xWindow (Snes9xConfig *config)
{    
#ifdef USE_OPENGL
    GdkGLConfig *glconfig;
    
    int attribs[] = 
    {
            GDK_GL_RGBA,
            GDK_GL_DOUBLEBUFFER,
            GDK_GL_RED_SIZE, 8,
            GDK_GL_GREEN_SIZE, 8,
            GDK_GL_BLUE_SIZE, 8,
            GDK_GL_ALPHA_SIZE, 8,
            GDK_GL_DEPTH_SIZE, 24,
            GDK_GL_ATTRIB_LIST_NONE
    };
#endif
    
    user_pause = 0;
    sys_pause = 0;
    last_width = -1;
    last_height = -1;
    is_fullscreen = 0;
    
    this->config = config;
    
    cursor = NULL;
    glade = glade_xml_new_from_buffer (_binary_snes9x_glade_start,
                                       _binary_snes9x_glade_end -
                                           _binary_snes9x_glade_start,
                                       "main_window",
                                       NULL); 
        
    
    window = GTK_WIDGET (glade_xml_get_widget (glade, "main_window"));
         
    icon = gdk_pixbuf_new_from_inline (-1,
                                       app_icon,
                                       FALSE,
                                       NULL); 
    
    gtk_window_set_default_icon (icon); 
    
    drawing_area = GTK_DRAWING_AREA 
                    (glade_xml_get_widget (glade, "drawingarea"));

    gtk_widget_set_double_buffered (GTK_WIDGET (drawing_area), FALSE);
    
#ifdef USE_OPENGL

    glconfig = gdk_gl_config_new (attribs);
    gtk_widget_set_gl_capability (GTK_WIDGET (drawing_area),
                                  glconfig,
                                  NULL,
                                  TRUE,
                                  GDK_GL_RGBA_TYPE);
    
    gtk_widget_realize (GTK_WIDGET (drawing_area));
    
#endif  
    
    gtk_widget_realize (window);
    
    glade_xml_signal_connect (glade, 
                              "main_window_delete_event",
                              G_CALLBACK (event_main_window_delete)); 
       
    glade_xml_signal_connect_data (glade,
                                   "on_preferences_item_activate",
                                   G_CALLBACK (snes9x_preferences_open),
                                   (gpointer) config);
    
    glade_xml_signal_connect_data (glade,
                                   "on_continue_item_activate",
                                   G_CALLBACK (event_continue_item_activate),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "on_pause_item_activate",
                                   G_CALLBACK (event_pause_item_activate),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "on_drawingarea_expose",
                                   G_CALLBACK (event_drawingarea_expose),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "main_window_key_press_event",
                                   G_CALLBACK (event_key),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "main_window_key_release_event",
                                   G_CALLBACK (event_key),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "on_double_size_item_activate",
                                   G_CALLBACK (event_double_size),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "on_native_size_item_activate",
                                   G_CALLBACK (event_native_size),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "on_fullscreen_item_activate",
                                   G_CALLBACK (event_fullscreen),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "on_open_rom_activate",
                                   G_CALLBACK (event_open_rom),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "on_reset_item_activate",
                                   G_CALLBACK (event_reset),
                                   (gpointer) this);

    glade_xml_signal_connect_data (glade,
                                   "hard_reset",
                                   G_CALLBACK (event_hard_reset),
                                   (gpointer) this);

    glade_xml_signal_connect_data (glade,
                                   "on_port_activate",
                                   G_CALLBACK (event_port),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "load_save_state",
                                   G_CALLBACK (event_load_state),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "load_state_file",
                                   G_CALLBACK (event_load_state_file),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "save_save_state",
                                   G_CALLBACK (event_save_state),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "save_state_file",
                                   G_CALLBACK (event_save_state_file),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "drawingarea_button_press",
                                   G_CALLBACK (event_button_press),
                                   (gpointer) this);
        
    glade_xml_signal_connect_data (glade,
                                   "drawingarea_button_release",
                                   G_CALLBACK (event_button_release),
                                   (gpointer) this);

    glade_xml_signal_connect_data (glade,
                                   "drawingarea_motion_notify",
                                   G_CALLBACK (event_motion_notify),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "save_spc",
                                   G_CALLBACK (event_save_spc),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "open_movie",
                                   G_CALLBACK (event_open_movie),
                                   (gpointer) this);

    glade_xml_signal_connect_data (glade,
                                   "stop_recording",
                                   G_CALLBACK (event_stop_recording),
                                   (gpointer) this);

    glade_xml_signal_connect_data (glade,
                                   "jump_to_frame",
                                   G_CALLBACK (event_jump_to_frame),
                                   (gpointer) this);

    glade_xml_signal_connect_data (glade,
                                   "record_movie",
                                   G_CALLBACK (event_record_movie),
                                   (gpointer) this);
    
    glade_xml_signal_connect_data (glade,
                                   "open_cheats",
                                   G_CALLBACK (event_open_cheats),
                                   (gpointer) this);
    

    
    if (config->window_width > 100 && config->window_height > 100)
    {
        gtk_window_resize (GTK_WINDOW (window), 
                           config->window_width, 
                           config->window_height);
    }
    
    return;
}

const char *
Snes9xWindow::open_movie_dialog (bool readonly)
{
    GtkWidget *dialog;
    GtkFileFilter *filter;
    char *filename;
    gint result;
    const char *extensions[] =
    {
            "*.smv", "*.SMV",
            NULL
    };
    
    this->pause_from_focus_change ();
    
    if (readonly)
    {
        dialog = gtk_file_chooser_dialog_new ("Open SNES Movie",
                                              GTK_WINDOW (this->window),
                                              GTK_FILE_CHOOSER_ACTION_OPEN,
                                              GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                              GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                              NULL);
    }
    else
    {
        char def[PATH_MAX];
        char default_name[PATH_MAX];
        char drive[_MAX_DRIVE];
        char dir[_MAX_DIR];
        char ext[_MAX_EXT];
    
        _splitpath (Memory.ROMFilename, drive, dir, def, ext);
              
        sprintf (default_name, "%s.smv", def);    
        
        dialog = gtk_file_chooser_dialog_new ("New SNES Movie",
                                              GTK_WINDOW (this->window),
                                              GTK_FILE_CHOOSER_ACTION_SAVE,
                                              GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                              GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                              NULL);

        gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), 
                                           default_name);                

    }

    filter = gtk_file_filter_new ();
    gtk_file_filter_set_name (filter, "SNES Movies");     
    for (int i = 0; extensions[i]; i++)
    {
        gtk_file_filter_add_pattern (filter, extensions[i]);
    }
    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
    
    filter = gtk_file_filter_new ();
    gtk_file_filter_set_name (filter, "All Files");
    gtk_file_filter_add_pattern (filter, "*.*");
    gtk_file_filter_add_pattern (filter, "*");
    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
    
    gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
                                         S9xGetDirectory (SRAM_DIR));

    result = gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_hide_all (dialog);
    
    if (result == GTK_RESPONSE_ACCEPT)   
    {
        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
    }   
    else
    {
        filename = strdup ("");
    }
    
    gtk_widget_destroy (dialog);
    
    this->unpause_from_focus_change ();
    
    return filename;
}

void
Snes9xWindow::open_rom_dialog ()
{
    GtkWidget *dialog;
    GtkFileFilter *filter;
    char *filename;
    gint result;
    const char *extensions[] =
    {
            "*.smc", "*.SMC", "*.fig", "*.FIG", "*.sfc", "*.SFC",
            "*.jma", "*.JMA", "*.zip", "*.ZIP", "*.gd3", "*.GD3",
            "*.swc", "*.SWC",
            NULL
    };
    
    this->pause_from_focus_change ();
    
    dialog = gtk_file_chooser_dialog_new ("Open SNES ROM",
                                          GTK_WINDOW (this->window),
                                          GTK_FILE_CHOOSER_ACTION_OPEN,
                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                          GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                          NULL);
    
    filter = gtk_file_filter_new ();
    gtk_file_filter_set_name (filter, "SNES ROMs");     
    for (int i = 0; extensions[i]; i++)
    {
        gtk_file_filter_add_pattern (filter, extensions[i]);
    }
    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
    
    filter = gtk_file_filter_new ();
    gtk_file_filter_set_name (filter, "All Files");
    gtk_file_filter_add_pattern (filter, "*.*");
    gtk_file_filter_add_pattern (filter, "*");
    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
    
    if (strcmp (config->last_directory, ""))
    {
        gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
                                             config->last_directory);
    }
    
    result = gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_hide_all (dialog);
    
    if (result == GTK_RESPONSE_ACCEPT)   
    {
        char *directory;
        
        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
        directory = 
            gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
        strncpy (config->last_directory, directory, PATH_MAX);
        g_free (directory);
        
        try_open_rom (filename);
        
        g_free (filename);
    }
    
    else
    {
    }
    
    gtk_widget_destroy (dialog);
    
    this->unpause_from_focus_change ();
    
    return;
}

int
Snes9xWindow::try_open_rom (const char *filename)
{
    this->pause_from_focus_change ();
    
    if (S9xOpenROM (filename))
    {
        GtkWidget *msg;

        msg = gtk_message_dialog_new (GTK_WINDOW (this->window),
                                      GTK_DIALOG_DESTROY_WITH_PARENT,
                                      GTK_MESSAGE_ERROR,
                                      GTK_BUTTONS_CLOSE,
                                      "Couldn't load file '%s'",
                                      filename);
        gtk_dialog_run (GTK_DIALOG (msg));
        gtk_widget_destroy (msg);

        S9xNoROMLoaded ();
        
        return 1;
    }
    else
    {   
        GtkRecentData recent_data =
        {
                NULL,
                (gchar *) "SNES ROM",
                (gchar *) "application/x-snes-rom",
                (gchar *) "Snes9x",
                NULL,
                NULL,
                FALSE
        };
        gchar *u_filename;

        u_filename = g_filename_to_uri (filename, NULL, NULL);

        recent_data.app_exec = g_strjoin (" ", 
                                          g_get_prgname (), 
                                          "%f", 
                                          NULL);
 
        gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
                                     u_filename,
                                     &recent_data);

        g_free (recent_data.app_exec);
        g_free (u_filename);
        
        this->unpause_from_user ();

    }
    
    this->unpause_from_focus_change ();
    
    return 0;
}

void
Snes9xWindow::load_state_dialog ()
{
    GtkWidget *dialog;
    GtkFileFilter *filter;
    char *filename;
    gint result;
    
    this->pause_from_focus_change ();
    
    dialog = gtk_file_chooser_dialog_new ("Load Saved State",
                                          GTK_WINDOW (this->window),
                                          GTK_FILE_CHOOSER_ACTION_OPEN,
                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                          GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                          NULL);
    
    gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), 
                                         S9xGetDirectory (SNAPSHOT_DIR));
    
    filter = gtk_file_filter_new ();
    gtk_file_filter_set_name (filter, "Save States");
    gtk_file_filter_add_pattern (filter, "*.sst");
    gtk_file_filter_add_pattern (filter, "*.zst");
    gtk_file_filter_add_pattern (filter, "*.ZST");
    gtk_file_filter_add_pattern (filter, "*.000");
    gtk_file_filter_add_pattern (filter, "*.001");
    gtk_file_filter_add_pattern (filter, "*.002");
    gtk_file_filter_add_pattern (filter, "*.003");
    gtk_file_filter_add_pattern (filter, "*.004");
    gtk_file_filter_add_pattern (filter, "*.005");
    gtk_file_filter_add_pattern (filter, "*.006");
    gtk_file_filter_add_pattern (filter, "*.007");
    gtk_file_filter_add_pattern (filter, "*.008");
    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
    
    filter = gtk_file_filter_new ();
    gtk_file_filter_set_name (filter, "All Files");
    gtk_file_filter_add_pattern (filter, "*.*");
    gtk_file_filter_add_pattern (filter, "*");
    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
    
    result = gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_hide_all (dialog);
    
    if (result == GTK_RESPONSE_ACCEPT)   
    {
        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
        
        S9xLoadState (filename);
        
        g_free (filename);
    }
    
    else
    {
    }
    
    gtk_widget_destroy (dialog);
    
    this->unpause_from_focus_change ();
    
    return;
}

void
Snes9xWindow::movie_seek_dialog (void)
{
    GladeXML *seek_dialog_xml;
    GtkWidget *seek_dialog;
    char str[1024];
    gint result;
    
    if (!S9xMovieActive ())
        return;
    
    pause_from_focus_change ();
    
    seek_dialog_xml = glade_xml_new_from_buffer (_binary_snes9x_glade_start,
                                                 _binary_snes9x_glade_end -
                                                     _binary_snes9x_glade_start,
                                                 "frame_advance_dialog",
                                                 NULL);
    
    seek_dialog = glade_xml_get_widget (seek_dialog_xml,
                                        "frame_advance_dialog");
    
    snprintf (str, 1024, 
              "The current frame in the movie is <b>%d</b>.",
              S9xMovieGetFrameCounter ());
    gtk_label_set_label (GTK_LABEL (
                             glade_xml_get_widget (seek_dialog_xml,
                                                   "current_frame_label")),
                         str);
    
    snprintf (str, 1024,
              "%d",
              S9xMovieGetFrameCounter ());
    gtk_entry_set_text (GTK_ENTRY (glade_xml_get_widget (seek_dialog_xml,
                                                         "frame_entry")),
                        str);
    
    result = gtk_dialog_run (GTK_DIALOG (seek_dialog));
    
    int entry_value = 
            atoi (
                gtk_entry_get_text (
                    GTK_ENTRY (
                        glade_xml_get_widget (
                            seek_dialog_xml, "frame_entry"))));
    
    switch (result)
    {
        case GTK_RESPONSE_OK:

            if (entry_value > 0 && 
                entry_value > (int) S9xMovieGetFrameCounter ())
            {
                Settings.HighSpeedSeek = 
                    entry_value - S9xMovieGetFrameCounter ();
            }
            
            break;
    }
    
    gtk_widget_destroy (seek_dialog);
   
    unpause_from_focus_change ();
    
    return;
}

void
Snes9xWindow::save_state_dialog ()
{
    GtkWidget *dialog;
    GtkFileFilter *filter;
    char *filename;
    gint result;
    char def[PATH_MAX];
    char default_name[PATH_MAX];
    char drive[_MAX_DRIVE];
    char dir[_MAX_DIR];
    char ext[_MAX_EXT];
    
    this->pause_from_focus_change ();
    
    _splitpath (Memory.ROMFilename, drive, dir, def, ext);
      
    sprintf (default_name, "%s.sst", def);
    
    dialog = gtk_file_chooser_dialog_new ("Save State",
                                          GTK_WINDOW (this->window),
                                          GTK_FILE_CHOOSER_ACTION_SAVE,
                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                          GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
                                          NULL);
    
    gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), 
                                         S9xGetDirectory (SNAPSHOT_DIR));
    gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), 
                                       default_name);
    
    filter = gtk_file_filter_new ();
    gtk_file_filter_set_name (filter, "Save States");
    gtk_file_filter_add_pattern (filter, "*.sst");
    gtk_file_filter_add_pattern (filter, "*.zst");
    gtk_file_filter_add_pattern (filter, "*.ZST");
    gtk_file_filter_add_pattern (filter, "*.000");
    gtk_file_filter_add_pattern (filter, "*.001");
    gtk_file_filter_add_pattern (filter, "*.002");
    gtk_file_filter_add_pattern (filter, "*.003");
    gtk_file_filter_add_pattern (filter, "*.004");
    gtk_file_filter_add_pattern (filter, "*.005");
    gtk_file_filter_add_pattern (filter, "*.006");
    gtk_file_filter_add_pattern (filter, "*.007");
    gtk_file_filter_add_pattern (filter, "*.008");
    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
    
    filter = gtk_file_filter_new ();
    gtk_file_filter_set_name (filter, "All Files");
    gtk_file_filter_add_pattern (filter, "*.*");
    gtk_file_filter_add_pattern (filter, "*");
    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
    
    result = gtk_dialog_run (GTK_DIALOG (dialog));
    
    gtk_widget_hide_all (dialog);
    
    if (result == GTK_RESPONSE_ACCEPT)   
    {
        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
        
        S9xSaveState (filename);
        
        g_free (filename);
    }
    
    else
    {
    }
    
    gtk_widget_destroy (dialog);
    
    this->unpause_from_focus_change ();
    
    return;
}

void
Snes9xWindow::save_spc_dialog ()
{
    GtkWidget *dialog;
    GtkFileFilter *filter;
    char *filename;
    gint result;
    char def[PATH_MAX];
    char default_name[PATH_MAX];
    char drive[_MAX_DRIVE];
    char dir[_MAX_DIR];
    char ext[_MAX_EXT];
    
    this->pause_from_focus_change ();
    
    _splitpath (Memory.ROMFilename, drive, dir, def, ext);
      
    sprintf (default_name, "%s.spc", def);
    
    dialog = gtk_file_chooser_dialog_new ("Save SPC file...",
                                          GTK_WINDOW (this->window),
                                          GTK_FILE_CHOOSER_ACTION_SAVE,
                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                          GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
                                          NULL);
    
    gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), 
                                         S9xGetDirectory (SNAPSHOT_DIR));
    gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), 
                                       default_name);
    
    filter = gtk_file_filter_new ();
    gtk_file_filter_set_name (filter, "SPC Files");
    gtk_file_filter_add_pattern (filter, "*.spc");
    gtk_file_filter_add_pattern (filter, "*.SPC");
    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
    
    filter = gtk_file_filter_new ();
    gtk_file_filter_set_name (filter, "All Files");
    gtk_file_filter_add_pattern (filter, "*.*");
    gtk_file_filter_add_pattern (filter, "*");
    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
    
    result = gtk_dialog_run (GTK_DIALOG (dialog));
    
    gtk_widget_hide_all (dialog);
    
    if (result == GTK_RESPONSE_ACCEPT)   
    {
        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
        
        if (S9xSPCDump (filename))
        {
            /* Success ? */
        }
        else
        {
            GtkWidget *msg;
            
            msg = gtk_message_dialog_new (GTK_WINDOW (this->window),
                                          GTK_DIALOG_DESTROY_WITH_PARENT,
                                          GTK_MESSAGE_ERROR,
                                          GTK_BUTTONS_CLOSE,
                                          "Couldn't save SPC file '%s'",
                                          filename);
            gtk_dialog_run (GTK_DIALOG (msg));
            gtk_widget_destroy (msg);
        }
        
        g_free (filename);
    }
    
    else
    {
    }
    
    gtk_widget_destroy (dialog);
    
    this->unpause_from_focus_change ();
    
    return;
}

void
Snes9xWindow::set_menu_item_selected (const char *name)
{
    GtkCheckMenuItem *item;
    
    item = GTK_CHECK_MENU_ITEM (glade_xml_get_widget (glade, name));
    
    gtk_check_menu_item_set_active (item, 1);
    
    return;
}

void
Snes9xWindow::update_statusbar (void)
{
    GtkStatusbar *bar =
            GTK_STATUSBAR (glade_xml_get_widget (glade, "statusbar"));
    char msg[256];
    
    if (!config->rom_loaded)
    {
        snprintf (msg, 256, "No ROM Loaded");
    }
    else
    {
        snprintf (msg, 
                  256, 
                  "%s - %s", 
                  Memory.ROMName,
                  is_paused () ? "Paused" : "Running");
    }
    
    gtk_statusbar_pop (bar, gtk_statusbar_get_context_id (bar, "none"));
    gtk_statusbar_push (bar, gtk_statusbar_get_context_id (bar, "none"), msg);    
    
    return;
}

void
Snes9xWindow::set_rom_loaded_state (void)
{
    enable_widget ("continue_item", 1);
    enable_widget ("pause_item", 1);
    enable_widget ("reset_item", 1);
    enable_widget ("controller_ports_item", 1);
    enable_widget ("fullscreen_item", 1);
    enable_widget ("load_state_item", 1);
    enable_widget ("save_state_item", 1);
    enable_widget ("save_spc_item", 1);
    enable_widget ("hard_reset_item", 1);
    enable_widget ("record_movie_item", 1);
    enable_widget ("stop_recording_item", 1);
    enable_widget ("open_movie_item", 1);
    enable_widget ("jump_to_frame_item", 1);
    enable_widget ("cheats_item", 1);
    
    hide_mouse_cursor ();
    propagate_pause_state ();
    
    top_level->last_width = top_level->last_height = SIZE_FLAG_DIRTY;
    
    if (config->full_screen_on_open)
    {
        if (!is_fullscreen)
            enter_fullscreen_mode ();
    }
    
    return;
}

void
Snes9xWindow::set_no_rom_loaded_state (void)
{
    enable_widget ("continue_item", 0);
    enable_widget ("pause_item", 0);
    enable_widget ("reset_item", 0);
    enable_widget ("controller_ports_item", 0);
    enable_widget ("fullscreen_item", 0);
    enable_widget ("load_state_item", 0);
    enable_widget ("save_state_item", 0);
    enable_widget ("save_spc_item", 0);
    enable_widget ("hard_reset_item", 0);
    enable_widget ("record_movie_item", 0);
    enable_widget ("stop_recording_item", 0);
    enable_widget ("open_movie_item", 0);
    enable_widget ("jump_to_frame_item", 0);
    enable_widget ("cheats_item", 0);
    
    show_mouse_cursor ();
    propagate_pause_state ();
    
    return;
}

void
Snes9xWindow::set_mouseable_area (int x, int y, int width, int height)
{
    mouse_region_x = x;
    mouse_region_y = y;
    mouse_region_width = width;
    mouse_region_height = height;
    
    return;
}

void
Snes9xWindow::toggle_fullscreen_mode (void)
{
    if (is_fullscreen)
        leave_fullscreen_mode ();
    else
        enter_fullscreen_mode ();
}

void
Snes9xWindow::enter_fullscreen_mode (void)
{
    S9xSoundStop ();
    
    if (!config->rom_loaded || is_fullscreen)
        return;
    
    gtk_window_get_size (GTK_WINDOW (window), &window_width, &window_height);
    gtk_window_get_position (GTK_WINDOW (window), &window_pos_x, &window_pos_y);
               
    if (config->change_display_resolution)
    {
        XRRSetScreenConfig (GDK_DISPLAY (),
                            config->xrr_config,
                            GDK_ROOT_WINDOW (),
                            (SizeID) config->xrr_index,
                            config->xrr_rotation,
                            CurrentTime);
    }

    gtk_window_fullscreen (GTK_WINDOW (window));
                
    gtk_widget_hide (glade_xml_get_widget (glade, "statusbar"));
    gtk_widget_hide (glade_xml_get_widget (glade, "menubar"));
    
    S9xSoundStart ();
    
    is_fullscreen = 1;
    
    return;
}

void
Snes9xWindow::leave_fullscreen_mode (void)
{
    if (!is_fullscreen)
        return;
    
    S9xSoundStop ();
        
    if (config->change_display_resolution && is_fullscreen)
    {
        XRRSetScreenConfig (GDK_DISPLAY (),
                            config->xrr_config,
                            GDK_ROOT_WINDOW (),
                            (SizeID) config->xrr_original_size,
                            config->xrr_rotation,
                            CurrentTime);
    }

    gtk_widget_show (glade_xml_get_widget (glade, "statusbar"));
    gtk_widget_show (glade_xml_get_widget (glade, "menubar"));
    gtk_window_unfullscreen (GTK_WINDOW (window));    
      
    S9xSoundStart ();
    
    is_fullscreen = 0;
    
    return;
}

void
Snes9xWindow::hide_mouse_cursor (void)
{
    GdkPixmap *cursor_pixmap;
    GdkGC *gc;
    GdkColor fg = { 0, 0, 0, 0 };
    GdkColor bg = { 0, 0, 0, 0 };
    
    if (!cursor)
    {
        cursor_pixmap = gdk_pixmap_new (NULL, 1, 1, 1);
        gc = gdk_gc_new (GDK_DRAWABLE (cursor_pixmap));
        gdk_gc_set_foreground (gc, &fg);
        gdk_draw_point (GDK_DRAWABLE (cursor_pixmap),
                        gc,
                        0, 0);
     
        cursor = gdk_cursor_new_from_pixmap (cursor_pixmap, 
                                             cursor_pixmap,
                                             &fg, &bg,
                                             0, 0);        
        g_object_unref (gc);
        g_object_unref (cursor_pixmap);
    }
     
    gdk_window_set_cursor (GTK_WIDGET (drawing_area)->window, cursor);
    
    return;
}

void
Snes9xWindow::show_mouse_cursor (void)
{
    gdk_window_set_cursor (GTK_WIDGET (drawing_area)->window, NULL);
    
    return;
}

void
Snes9xWindow::show (void)
{
    static int added_recent = 0;

    gtk_widget_show_all (window);
    
    if (!added_recent)
    {
        /* Add recent menu after showing window to avoid "No items" bug */
        recent_menu = gtk_recent_chooser_menu_new_for_manager (
                          gtk_recent_manager_get_default ());

        GtkRecentFilter *filter = gtk_recent_filter_new ();
        gtk_recent_filter_add_application (filter, "Snes9x");
        gtk_recent_chooser_set_local_only (GTK_RECENT_CHOOSER (recent_menu), 
                                           TRUE);
        gtk_recent_chooser_set_show_icons (GTK_RECENT_CHOOSER (recent_menu), 
                                           FALSE);
        gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (recent_menu), 
                                          GTK_RECENT_SORT_MRU);
        gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (recent_menu), 
                                       filter);

        gtk_menu_item_set_submenu (
            GTK_MENU_ITEM (
                glade_xml_get_widget (glade,
                                      "open_recent_item")),
                                      recent_menu);
        
        g_signal_connect (G_OBJECT (recent_menu), 
                          "item-activated", 
                          G_CALLBACK (event_recent_open), 
                          (gpointer) this);
    }
    return;
}

void
Snes9xWindow::propagate_pause_state (void)
{
    Settings.Paused = (sys_pause || user_pause || !(config->rom_loaded));
    
    if (!is_paused ())
    {
        S9xSoundStart ();
        if (config->rom_loaded)
                    enable_widget ("pause_item", TRUE); 
                
        hide_mouse_cursor ();
        
        S9xDisplayClearBuffers ();
    }
    else
    {
        S9xSoundStop ();
        enable_widget ("pause_item", FALSE); 
        
        show_mouse_cursor ();
    } 
    
    update_statusbar ();  
    
    return;
}

/* gui_[un]pause Handles when system needs to pause the emulator */
void
Snes9xWindow::pause_from_focus_change (void)
{
    leave_fullscreen_mode ();
    
    sys_pause++;
    
    propagate_pause_state ();
    
    return;
}

void
Snes9xWindow::unpause_from_focus_change (void)
{
    sys_pause--;
    propagate_pause_state ();
    
    return;
}

/* client_[un]pause Handles when user manually chooses to pause */
void
Snes9xWindow::pause_from_user (void)
{
    user_pause = TRUE;
    propagate_pause_state ();
    
    return;
}

void
Snes9xWindow::unpause_from_user (void)
{
    user_pause = FALSE;
    propagate_pause_state ();
    
    return;
}

unsigned char
Snes9xWindow::is_paused (void)
{
    if (user_pause || sys_pause || Settings.Paused || !(config->rom_loaded))
        return TRUE;
    return FALSE;
}

void
Snes9xWindow::enable_widget (const char *name, unsigned char state)
{
    GtkWidget *widget;
    
    widget = GTK_WIDGET (glade_xml_get_widget (glade, name));
    
    gtk_widget_set_sensitive (widget, state);
    
    return;
}

void
Snes9xWindow::set_menu_item_accel_to_binding (const char *name, 
                                              const char *binding)
{
    Binding bin;     
    char str[255];
    
    bin = S9xGetBindingByName (binding);
    
    snprintf (str, 255, "<Snes9x>/%s", name);
    if (!(bin & BINDING_KEY))
    {
        gtk_accel_map_change_entry (str,
                                    0,
                                    (GdkModifierType) 0,
                                    TRUE);
        return;
    }
    
    gtk_widget_set_accel_path (glade_xml_get_widget (glade, name),
                               str,
                               glade_xml_ensure_accel (glade));

    
    if (!gtk_accel_map_lookup_entry (str, NULL))
    {
        gtk_accel_map_add_entry (str, 
                                 BINDING_KEY_MASK & bin,
                                 (GdkModifierType) (
                                 (BINDING_CTRL  & bin ? GDK_CONTROL_MASK : 0) |
                                 (BINDING_ALT   & bin ? GDK_MOD1_MASK    : 0) |
                                 (BINDING_SHIFT & bin ? GDK_SHIFT_MASK   : 0)));

    }
    else
    {
        gtk_accel_map_change_entry (str,
                                    BINDING_KEY_MASK & bin,
                                    (GdkModifierType) (
                                    (BINDING_CTRL  & bin ? GDK_CONTROL_MASK : 0) |
                                    (BINDING_ALT   & bin ? GDK_MOD1_MASK    : 0) |
                                    (BINDING_SHIFT & bin ? GDK_SHIFT_MASK   : 0)),
                                    TRUE);
    }
                                
    return;
}

void
Snes9xWindow::update_accels (void)
{
    set_menu_item_accel_to_binding ("fullscreen_item", "GTK_fullscreen");
    set_menu_item_accel_to_binding ("reset_item", "SoftReset");
    set_menu_item_accel_to_binding ("save_state_0", "QuickSave000");
    set_menu_item_accel_to_binding ("save_state_1", "QuickSave001");
    set_menu_item_accel_to_binding ("save_state_2", "QuickSave002");
    set_menu_item_accel_to_binding ("save_state_3", "QuickSave003");
    set_menu_item_accel_to_binding ("save_state_4", "QuickSave004");
    set_menu_item_accel_to_binding ("save_state_5", "QuickSave005");
    set_menu_item_accel_to_binding ("save_state_6", "QuickSave006");
    set_menu_item_accel_to_binding ("save_state_7", "QuickSave007");
    set_menu_item_accel_to_binding ("save_state_8", "QuickSave008");
    set_menu_item_accel_to_binding ("load_state_0", "QuickLoad000");
    set_menu_item_accel_to_binding ("load_state_1", "QuickLoad001");
    set_menu_item_accel_to_binding ("load_state_2", "QuickLoad002");
    set_menu_item_accel_to_binding ("load_state_3", "QuickLoad003");
    set_menu_item_accel_to_binding ("load_state_4", "QuickLoad004");
    set_menu_item_accel_to_binding ("load_state_5", "QuickLoad005");
    set_menu_item_accel_to_binding ("load_state_6", "QuickLoad006");
    set_menu_item_accel_to_binding ("load_state_7", "QuickLoad007");
    set_menu_item_accel_to_binding ("load_state_8", "QuickLoad008");
    set_menu_item_accel_to_binding ("pause_item", "GTK_pause");
    set_menu_item_accel_to_binding ("save_spc_item", "GTK_save_spc");
    set_menu_item_accel_to_binding ("open_rom_item", "GTK_open_rom");
    set_menu_item_accel_to_binding ("record_movie_item", "BeginRecordingMovie");
    set_menu_item_accel_to_binding ("open_movie_item", "LoadMovie");
    set_menu_item_accel_to_binding ("stop_recording_item", "EndRecordingMovie");
    set_menu_item_accel_to_binding ("jump_to_frame_item", "GTK_seek_to_frame");
    set_menu_item_accel_to_binding ("reset_item", "SoftReset");
    set_menu_item_accel_to_binding ("hard_reset_item", "Reset");

    return;
}
